package ar.unc.famaf.pln.giveselector.classifier;

import java.io.Serializable;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import ar.unc.famaf.pln.giveselector.context.Context;
import ar.unc.famaf.pln.giveselector.context.Response;
import ar.unc.famaf.pln.giveselector.context.Utterance;

/**
 * Extends the Utterance class with information about 
 * its occurrences that LogAnalyser has founded. 
 * The LogAnalyser class creates a database of UtteranceP objects.
 * @author David Racca
 *
 */
@SuppressWarnings("serial")
public class UtteranceP extends Utterance implements Serializable 
{
	private List<Occurrence> occurrences;


	/**
	 * Creates a new UtteranceP object with an empty array of Occurrence objects.
	 */
	public UtteranceP(String text, Context features, Response response) 
	{
		super(text, features, response);
		occurrences = new ArrayList<Occurrence>();
	}


	/**
	 * Creates a new UtteranceP object from an existent Utterance object
	 * with an Empty array of Occurrence objects.
	 */
	public UtteranceP(Utterance utt) 
	{
		super(utt.getId(), utt.getText(), utt.getContext(), utt.getResponse());
		occurrences = new ArrayList<Occurrence>();
	}


	/**
	 * @return The list of Occurrence objects for this instance.
	 */
	public List<Occurrence> getOccurrences()
	{
		return occurrences;
	}


	/**
	 * Retrieves the Occurrence object that has the given time-stamp
	 * @return The Occurrence with the given time stamp or
	 * null if such object does not exist in the list of related Occurrence
	 * objects of this instance.
	 */
	public Occurrence getOccurrence(Date timestamp)
	{
		Occurrence result = null;
		for (Occurrence o : occurrences)
		{
			if (o.getTimestamp().equals(timestamp))
			{
				result = o;
				break;
			}
		}
		return result; 
	}


	/**
	 * If the given object is not null, this method appends such 
	 * occurrence to this UtteranceP.
	 */
	public void addOccurrence(Occurrence occurrence) 
	{
		if (occurrence != null)
			occurrences.add(occurrence);
	}


	/**
	 * Appends all the given occurrences to this UtteranceP.
	 */
	public void addOccurrences(List<Occurrence> occurrences)
	{
		if (occurrences != null)
			this.occurrences.addAll(occurrences);
	}


	/**
	 * @return a String representation for this object
	 */
	public String toString() 
	{
		return super.toString() + " : " + occurrences;
	}

}
